package com.zhongshengnetwork.rightbe.wzt.view;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
//import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wang.avi.AVLoadingIndicatorView;
import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.BuildConfig;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.BaseAnalyticsFragment;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.ScreenUtils;
import com.zhongshengnetwork.rightbe.common.customview.CustomDialog;
import com.zhongshengnetwork.rightbe.common.topgrid.activity.WZTGridActivity;
import com.zhongshengnetwork.rightbe.gsonmodel.ClassifyModel;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.WeiShuInfoDetailModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;
import com.zhongshengnetwork.rightbe.lang.activity.BookmarkActivity;
import com.zhongshengnetwork.rightbe.lang.activity.BookmarkCardActivity;
import com.zhongshengnetwork.rightbe.lang.model.BookmarkModel;
import com.zhongshengnetwork.rightbe.wzt.activity.ClassifyActivity;
import com.zhongshengnetwork.rightbe.wzt.activity.WZTActivity;
import com.zhongshengnetwork.rightbe.wzt.activity.WZTListActivity;
import com.zhongshengnetwork.rightbe.wzt.activity.WZTListOfClassifyActivity;
import com.zhongshengnetwork.rightbe.wzt.activity.WZTSearchActivity;
import com.zhongshengnetwork.rightbe.wzt.model.TwoTuple;
import com.zhongshengnetwork.rightbe.wzt.model.WZTUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.zhongshengnetwork.rightbe.MainActivity.REQUEST_WZT_MENU;

/**
 * Created by luojingxing on 2019/11/4.
 */

public class WZTFindFragment extends BaseAnalyticsFragment {

    private final int linecount = 3;
    public String userid = null;
    public int dataType = 0;
    private int pageindex = 0;
    private static final int Records = 20;
    private boolean isHasMore = false;
    private List<WeiShuInfoDetailModel> list;
    private List<String> headerList;
    private int lineNumber = 0;
    private ListView listview;
    private MyAdapter adapter;
    private View rootView;//缓存Fragment view
    private int imgW = 0;
    private boolean isGetting = false;
    private ImageView no_data_img;
    private RefreshLayout mRefreshLayout;
    private RelativeLayout wzt_homefind;
    private TextView classify_text;

    //加载动画
    private AVLoadingIndicatorView avi;
    private RelativeLayout search_top;
    private LinearLayout my_classify_layout;

    private void calculateImageWidth(){
        int width = ScreenUtils.getScreenWidth(getActivity());
        float density = ScreenUtils.getScreenDensity(getActivity());
        imgW = (int) ((width-40*density)/linecount);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        calculateImageWidth();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.wzt_homefind_layout, container, false);
            pageindex = 0;
            if (list == null){
                list = new ArrayList<WeiShuInfoDetailModel>();
                int width = ScreenUtils.getScreenWidth(getActivity());
                float density = ScreenUtils.getScreenDensity(getActivity());
                imgW = (int) ((width-40*density)/linecount);
            }

            if (headerList == null){
                headerList = new ArrayList<String>();
            }

            no_data_img = (ImageView)rootView.findViewById(R.id.no_data_img);
            listview=(ListView) rootView.findViewById(R.id.wzt_listview);
            adapter=new MyAdapter();
            listview.setAdapter(adapter);
            listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Log.e("TAG","position="+position);
                }
            });

            mRefreshLayout = rootView.findViewById(R.id.refreshLayout);
            //设置 Header 为 贝塞尔雷达 样式
            mRefreshLayout.setRefreshHeader(new BezierRadarHeader(getActivity()).setEnableHorizontalDrag(true));//BezierRadarHeader
            //设置 Footer 为 球脉冲 样式
            mRefreshLayout.setRefreshFooter(new ClassicsFooter(getActivity()).setSpinnerStyle(SpinnerStyle.FixedBehind));//BallPulseFooter ClassicsFooter
            mRefreshLayout.setPrimaryColors(AppThemeUtils.getInstance().getRefreshBackgroundColor(),AppThemeUtils.getInstance().getRefreshAccessColor());
            mRefreshLayout.setEnableRefresh(true);//启用刷新
            mRefreshLayout.setEnableLoadMore(false);//启用加载
            //刷新
            mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(RefreshLayout refreshlayout) {
                    //refreshlayout.finishRefresh();
                    pageindex = 0;
                    getData();
                }
            });
            mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                    refreshLayout.finishLoadMore();
                    //getData();
                }
            });

            search_top = (RelativeLayout) rootView.findViewById(R.id.search_top);
            search_top.setClickable(true);
            search_top.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getActivity() == null){
                        return;
                    }
                    Intent intent = new Intent(getActivity(), WZTSearchActivity.class);
                    startActivity(intent);
                }
            });

            TextView classify_text = (TextView) rootView.findViewById(R.id.classify_text);
            classify_text.setText("分类推荐");
            my_classify_layout = (LinearLayout) rootView.findViewById(R.id.my_classify_layout);
            my_classify_layout.setVisibility(View.GONE);
            my_classify_layout.setClickable(true);
            my_classify_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                        Intent intent = new Intent(getActivity(), ClassifyActivity.class);
                        intent.putExtra("title", "分类推荐");
                        intent.putExtra("dataType", 1);
                        startActivity(intent);

                }
            });


            wzt_homefind = (RelativeLayout) rootView.findViewById(R.id.wzt_homefind);
            wzt_homefind.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            listview.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            listview.setDivider(new ColorDrawable(AppThemeUtils.getInstance().getMainColor()));
            listview.setDividerHeight(0);//AppThemeUtils.DividerHeight

            LinearLayout top_layout = (LinearLayout) rootView.findViewById(R.id.top_layout);
            top_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            my_classify_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            classify_text = (TextView) rootView.findViewById(R.id.classify_text);
            classify_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
            search_top.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            RelativeLayout search_shape_layout = (RelativeLayout) rootView.findViewById(R.id.search_shape_layout);
            search_shape_layout.setBackgroundResource(AppThemeUtils.getInstance().getSearchBackground());

            getData();
        }else {
            //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }

        return rootView;
    }

    public void getFirstData(boolean isFirst){
        if (listview == null || list == null){
            return;
        }
        if (list.size() == 0){
            //listview.startRefresh();
            if (mRefreshLayout != null){
                pageindex = 0;
                mRefreshLayout.autoRefresh();
            }
        }else {
            if (isFirst == true){
                //listview.smoothScrollToPosition(0);
                listview.setSelection(0);
            }
        }
    }

    private void getData(){
        if (listview == null){
            return;
        }
        if (isGetting == true){
            if (mRefreshLayout.getState() == RefreshState.Refreshing){
                mRefreshLayout.finishRefresh();
            }else {
                mRefreshLayout.finishLoadMore();
            }
            return;
        }
        isGetting = true;
        new Thread(){
            @Override
            public void run() {
                super.run();

                /*
                //首次从缓存里面获取数据
                if (list.size() == 0 && pageindex == 0){
                    String data = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.WZTFindID,"");
                    if (!CommonUtils.isEmpty(data)){
                        List<WeiShuInfoDetailModel> l = GsonTools.getWeiShuFindHome(data);
                        if (l != null && l.size() > 0){
                            TwoTuple twoTuple = GsonTools.getWeiShuFindHomeHeader(data);
                            if (twoTuple != null){
                                List<String> hl = CommonUtils.stringToArrayList((String) twoTuple.getPos());
                                if (hl != null && hl.size() > 0){
                                    headerList.clear();
                                    headerList.addAll(hl);
                                }
                                lineNumber = (int)twoTuple.count;
                                Log.e("TAG","发现lineNumber="+lineNumber);
                                if (getActivity() == null){
                                    return;
                                }
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (l != null && l.size() > 0){
                                            list.addAll(l);
                                        }
                                        no_data_img.setVisibility(View.INVISIBLE);
                                        adapter.notifyDataSetChanged();
                                    }
                                });
                            }
                        }
                    }
                }
                */

                final HashMap<String, String> map = new HashMap<String, String>();
                String url = "weishu/theme/find/home.do";
                map.put(APIKey.tokenKey, CommonUtils.formatString(CustomApplication.loginModel.getToken()));
                map.put("pageIndex",pageindex+"");
                map.put("pageRecord",Records+"");
                map.put("dataText", BuildConfig.VERSION_CODE+"");
                map.put("dataType","0");//返回分类与收藏夹
                map.put("mode", SPManageUtils.getInstance(CustomApplication.mContext).getSPInt(AdvertUtils.UIModeKey, AdvertUtils.UI_Simple_Mode)+"");
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(final String data) {
                        isGetting = false;
                        if (getActivity() != null){
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (mRefreshLayout.getState() == RefreshState.Refreshing){
                                        list.clear();
                                        mRefreshLayout.finishRefresh();
                                    }else {
                                        mRefreshLayout.finishLoadMore();
                                    }
                                    CommonModel commonModel = GsonTools.getCommonModel(data);
                                    if (commonModel.getFlag().equals("1")){
                                        List<WeiShuInfoDetailModel> l = GsonTools.getWeiShuFindHome(data);
                                        if (l != null && l.size() > 0){
                                            TwoTuple twoTuple = GsonTools.getWeiShuFindHomeHeader(data);
                                            if (twoTuple != null){
                                                List<String> hl = CommonUtils.stringToArrayList((String) twoTuple.getPos());
                                                if (hl != null && hl.size() > 0){
                                                    headerList.clear();
                                                    headerList.addAll(hl);
                                                }
                                                lineNumber = (int)twoTuple.count;
                                                Log.e("TAG","发现lineNumber="+lineNumber);
                                            }
                                            no_data_img.setVisibility(View.INVISIBLE);
                                            mRefreshLayout.setEnableLoadMore(false);
                                            list.clear();
                                            list.addAll(l);
                                            Log.e("TAG","发现实际数量="+l.size());
                                        }else {
                                            isHasMore = false;
                                            //listview.setPullLoadEnable(false);
                                            mRefreshLayout.setEnableLoadMore(false);
                                        }
                                        adapter.notifyDataSetChanged();
                                        if (pageindex == 0){
                                            //listview.smoothScrollToPosition(0);
                                            if (list.size() > 0){
                                                /*
                                                if (list.size() > 0){
                                                    SPManageUtils.getInstance(CustomApplication.mContext).putSPString(AdvertUtils.WZTFindID, data);
                                                }
                                                */
                                            }
                                            listview.setSelection(0);
                                        }
                                    }else {
                                        CustomApplication.showTip(commonModel, getActivity());
                                    }
                                }
                            });
                        }

                    }

                    @Override
                    public void onError(String msg) {
                        isGetting = false;
                        if (getActivity() != null){
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (mRefreshLayout.getState() == RefreshState.Refreshing){
                                        mRefreshLayout.finishRefresh();
                                    }else {
                                        mRefreshLayout.finishLoadMore();
                                    }
                                }
                            });
                        }
                    }
                });
            }
        }.start();

    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return lineNumber;
        }


        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            return super.isEnabled(position);
        }

        /**
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            if (headerList.contains(position+"") == true){
                int index = linecount*position;
                View view = LayoutInflater.from(CustomApplication.mContext).inflate(R.layout.wzt_find_header_layout, null);;

                LinearLayout wzt_find_header = (LinearLayout) view.findViewById(R.id.wzt_find_header);
                wzt_find_header.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());

                TextView type_layout_title = (TextView) view.findViewById(R.id.type_layout_title);
                LinearLayout type_layout = (LinearLayout) view.findViewById(R.id.type_layout);
                type_layout.setTag(index);
                type_layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClickHeaderItem(v);
                    }
                });
                WeiShuInfoDetailModel model = list.get(index);
                type_layout_title.setText(model.getTitle());
                type_layout_title.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
                Log.e("标题",model.getTitle());
                return view;
            }

            //convertView的复用
            View view = View.inflate(getActivity(), R.layout.wzt_listview_item,null);;

            LinearLayout wzt_layout_1 = (LinearLayout) view.findViewById(R.id.wzt_layout_1);
            LinearLayout wzt_layout_2 = (LinearLayout) view.findViewById(R.id.wzt_layout_2);
            LinearLayout wzt_layout_3 = (LinearLayout) view.findViewById(R.id.wzt_layout_3);

            ImageView wzt_img1 = (ImageView) view.findViewById(R.id.wzt_img1);
            ImageView wzt_read_icon_1 = (ImageView) view.findViewById(R.id.wzt_read_icon_1);
            TextView wzt_readcount_1 = (TextView) view.findViewById(R.id.wzt_readcount_1);
            TextView private_text_1 = (TextView) view.findViewById(R.id.private_text_1);
            TextView wzt_text1 = (TextView) view.findViewById(R.id.wzt_text1);


            ImageView wzt_img2 = (ImageView) view.findViewById(R.id.wzt_img2);
            ImageView wzt_read_icon_2 = (ImageView) view.findViewById(R.id.wzt_read_icon_2);
            TextView wzt_readcount_2 = (TextView) view.findViewById(R.id.wzt_readcount_2);
            TextView private_text_2 = (TextView) view.findViewById(R.id.private_text_2);
            TextView wzt_text2 = (TextView) view.findViewById(R.id.wzt_text2);

            ImageView wzt_img3 = (ImageView) view.findViewById(R.id.wzt_img3);
            ImageView wzt_read_icon_3 = (ImageView) view.findViewById(R.id.wzt_read_icon_3);
            TextView wzt_readcount_3 = (TextView) view.findViewById(R.id.wzt_readcount_3);
            TextView private_text_3 = (TextView) view.findViewById(R.id.private_text_3);
            TextView wzt_text3 = (TextView) view.findViewById(R.id.wzt_text3);


            wzt_img1.setVisibility(View.GONE);
           wzt_layout_1.setVisibility(View.GONE);
            wzt_readcount_1.setVisibility(View.GONE);
            private_text_1.setVisibility(View.GONE);
            wzt_text1.setVisibility(View.GONE);


wzt_img2.setVisibility(View.GONE);
wzt_layout_2.setVisibility(View.GONE);
            wzt_readcount_2.setVisibility(View.GONE);
            private_text_2.setVisibility(View.GONE);
            wzt_text2.setVisibility(View.GONE);

            wzt_img3.setVisibility(View.GONE);
wzt_layout_3.setVisibility(View.GONE);
            wzt_readcount_3.setVisibility(View.GONE);
            private_text_3.setVisibility(View.GONE);
            wzt_text3.setVisibility(View.GONE);

            LinearLayout wzt_listview_item_layout = (LinearLayout) view.findViewById(R.id.wzt_listview_item_layout);
            wzt_listview_item_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            wzt_text1.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
            wzt_text2.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
            wzt_text3.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
            wzt_img1.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            wzt_img2.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            wzt_img3.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());

            int num = 0;
            int count = linecount*position;
            if (list.size()-count >= linecount){
                num = linecount;
            }else {
                while (list.size()-count > 0){
                    num+=1;
                    count+=1;
                }
            }
            int index = linecount*position;
            RequestOptions options = new RequestOptions().error(AppThemeUtils.getInstance().getDefaultImageBg()).bitmapTransform(new RoundedCorners(Constant.CornerRadius));//图片圆角
            for (int i = 0; i < num; i++){
                WeiShuInfoDetailModel model = list.get(index+i);
                if (model == null){
                    Log.e("TAG","没有内容的");
                    break;
                }
                if (CommonUtils.isEmpty(model.getUserid())){
                    Log.e("TAG","没有用户id");
                    break;
                }
                if (i == 0){
                    wzt_img1.setVisibility(View.VISIBLE);
                    wzt_layout_1.setVisibility(View.VISIBLE);
                    wzt_readcount_1.setVisibility(View.VISIBLE);
                    wzt_text1.setVisibility(View.VISIBLE);
                    iconImage(wzt_read_icon_1,model);
                    ViewGroup.LayoutParams params = wzt_img1.getLayoutParams();
                    params.width =imgW;
                    params.height = imgW;
                    wzt_img1.setLayoutParams(params);
                    params = wzt_text1.getLayoutParams();
                    params.width = imgW;
                    wzt_text1.setLayoutParams(params);
                    Glide.with(getActivity()).load(model.getCover()).apply(options).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(wzt_img1);
                    wzt_text1.setText(model.getTitle());
                    wzt_readcount_1.setText(CommonUtils.getDisplayCount(model.getRealcount()));
                    if (model.getVisible() == false){
                        private_text_1.setVisibility(View.VISIBLE);
                    }
                    wzt_layout_1.setTag(index+i);
                    wzt_layout_1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onClickItem(v);
                        }
                    });
                }else if (i == 1){
                    wzt_img2.setVisibility(View.VISIBLE);
                    wzt_layout_2.setVisibility(View.VISIBLE);
                    wzt_readcount_2.setVisibility(View.VISIBLE);
                    wzt_text2.setVisibility(View.VISIBLE);
                    iconImage(wzt_read_icon_2,model);
                    ViewGroup.LayoutParams params = wzt_img2.getLayoutParams();
                    params.width =imgW;
                    params.height = imgW;
                    wzt_img2.setLayoutParams(params);
                    params = wzt_text2.getLayoutParams();
                    params.width = imgW;
                    wzt_text2.setLayoutParams(params);
                    Glide.with(getActivity()).load(model.getCover()).apply(options).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(wzt_img2);
                    wzt_text2.setText(model.getTitle());
                    wzt_readcount_2.setText(CommonUtils.getDisplayCount(model.getRealcount()));
                    if (model.getVisible() == false){
                        private_text_2.setVisibility(View.VISIBLE);
                    }
                    wzt_layout_2.setTag(index+i);
                    wzt_layout_2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onClickItem(v);
                        }
                    });
                }else if (i == 2){
                    wzt_img3.setVisibility(View.VISIBLE);
                    wzt_layout_3.setVisibility(View.VISIBLE);
                    wzt_readcount_3.setVisibility(View.VISIBLE);
                    wzt_text3.setVisibility(View.VISIBLE);
                    iconImage(wzt_read_icon_3,model);
                    ViewGroup.LayoutParams params = wzt_img3.getLayoutParams();
                    params.width =imgW;
                    params.height = imgW;
                    wzt_img3.setLayoutParams(params);
                    params = wzt_text3.getLayoutParams();
                    params.width = imgW;
                    wzt_text3.setLayoutParams(params);
                    Glide.with(getActivity()).load(model.getCover()).apply(options).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(wzt_img3);
                    wzt_text3.setText(model.getTitle());
                    wzt_readcount_3.setText(CommonUtils.getDisplayCount(model.getRealcount()));
                    if (model.getVisible() == false){
                        private_text_3.setVisibility(View.VISIBLE);
                    }
                    wzt_layout_3.setTag(index+i);
                    wzt_layout_3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onClickItem(v);
                        }
                    });
                }
            }
            return view;
        }
    }

    private void onClickHeaderItem(View v){
        int index = (int)v.getTag();
        if (list == null || index >= list.size()){
            return;
        }
        WeiShuInfoDetailModel model = list.get(index);
        if (model.getType() == 10000){//分类
            Intent intent = new Intent(getActivity(), ClassifyActivity.class);
            intent.putExtra("title", model.getTitle());
            intent.putExtra("dataType", 1);
            startActivity(intent);
        }else if (model.getType() == 10001){//收藏夹
            Intent intent = new Intent(getActivity(), BookmarkActivity.class);
            intent.putExtra("title",model.getTitle());
            intent.putExtra("dataType","0");
            startActivity(intent);
        }else {
            Intent intent = new Intent(getActivity(), WZTListActivity.class);
            intent.putExtra("title",model.getTitle());
            intent.putExtra("dataType",Integer.valueOf(model.getType()+""));
            startActivity(intent);
        }
    }

    private void onClickItem(View v){

        int index = (int)v.getTag();
        if (list == null || index >= list.size()){
            return;
        }
        WeiShuInfoDetailModel model = list.get(index);
        if(model.getId() == null || model.getId() == 0){
            if (model.getBookmarkId() != null && model.getBookmarkId() > 0){//收藏夹
                BookmarkModel m = new BookmarkModel();
                m.setBookmarkName(model.getBookmarkName());
                m.setBookmarkDetail(model.getBookmarkDetail());
                m.setBookmarkId(model.getBookmarkId());
                m.setContentCount(model.getContentCount());
                m.setVisible(model.getVisible());
                m.setUserid(model.getUserid());//不允许编辑
                m.setHeader(model.getHeader());
                m.setNickname(model.getNickname());
                m.setIcon(model.getIcon());
                m.setContentCount(model.getContentCount());
                Intent intent = new Intent(getActivity() , BookmarkCardActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("model", m);//序列化
                intent.putExtras(bundle);//发送数据
                getActivity().startActivity(intent);
            }else {//分类
                ClassifyModel m = new ClassifyModel();
                m.setClassifyId(model.getClassifyId());
                m.setClassifyName(model.getClassifyName());
                m.setIcon(model.getIcon());
                m.setUserid("");//不允许编辑
                m.setVisible(model.getVisible());
                m.setWeishuCount(model.getWeishuCount());
                Intent intent = new Intent(getActivity(), WZTListOfClassifyActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("model", m);//序列化
                intent.putExtras(bundle);//发送数据
                startActivity(intent);
            }
            return;
        }

        WZTUtils.goToWZT(getActivity(), model);

    }

    private void iconImage(ImageView icon, WeiShuInfoDetailModel model){
        if(model.getId() == null || model.getId() == 0) {
            if (model.getBookmarkId() != null && model.getBookmarkId() > 0) {//收藏夹
                icon.setImageResource(R.drawable.white_collect);
            }else {//分类
                icon.setImageResource(R.drawable.number);
            }
        }else {//微纸条
            icon.setImageResource(R.drawable.wzt_browse);
        }
    }

}
